<?php

namespace catchAdmin\externalGoods\controller;

use catchAdmin\externalGoods\model\Goods as externalGoodsModel;
use catchAdmin\externalGoods\model\GoodsAttribute;
use catchAdmin\externalGoods\model\GoodsRule;
use catchAdmin\externalGoods\model\GoodsRule as externalGoodsRuleModel;
use catchAdmin\externalGoods\model\GoodsAttribute as externalGoodsAttribute;
use catchAdmin\externalGoods\model\TenantGoods as tenantGoodsModel;
use catchAdmin\externalGoods\model\TenantGoodsAttr as tenantGoodsAttrModel;
use catchAdmin\externalGoods\model\TenantGoodsRule as tenantGoodsRuleModel;
use catchAdmin\externalGoods\request\GoodsCreateRequest;
use catchAdmin\externalGoods\service\GoodsService;
use catchAdmin\product\service\CategoryService;
use catchAdmin\externalGoods\service\GoodsService as externalGoodsService;
use catcher\base\CatchRequest as Request;
use think\Exception;
use think\facade\Db;
use think\Request as ThinkRequest;
use catcher\CatchAdmin;
use catcher\CatchAuth;
use catcher\CatchResponse;
use houniao\api\v1\Goods as GoodsApi;

class Goods extends CatchAdmin
{

    public function __construct(externalGoodsModel $externalGoodsModel)
    {
        $this->externalGoodsModel = $externalGoodsModel;
    }

    //列表
    public function index(CatchAuth $auth, Request $request)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->externalGoodsModel->getList($user['tenant_id']));
    }

    //远程
    public function remote(CatchAuth $auth, Request $request)
    {
        $params = $request->filterEmptyField()->param();
//            'page_size' => $params['limit'] ?? 50,
//            'current_page' => $params['page'] ?? 1//前台

        $data = $params;
        $data['page_size'] = $params['limit'] ?? 50;
        $data['current_page'] = $params['page'] ?? 1;

        $response = (new GoodsApi)->goods($data);

        $goodsService = new externalGoodsService();

        $result = $goodsService->check($response['data']);

        return $result;
    }

    //同步
    public function sync(CatchAuth $auth, Request $request)
    {
        $params = $request->post('lists');

        $goodsService = new externalGoodsService();

//        try {
//            (new GoodsApi())->goodsSpecPrice([
//                'goods_spu'=>'HN10755000000001'
//            ]);
//        }catch (Exception $e){
//            return CatchResponse::fail($e->getMessage());
//        }

        //这里判断同步的是哪里的供应链
        if (empty($params)) {
            $params = (new GoodsApi)->goods([
                'page_size' => $params['limit'] ?? 50,//条目数量
                'current_page' => $params['page'] ?? 1//页码
            ])['data']['data'];
        }

//dd($params);

        $result = $goodsService->sync($params);

        return $result;
    }

    //创建商品
    public function create(CatchAuth $auth, GoodsCreateRequest $request)
    {
//        $arr = '[{"Action":"1","AcceptStation":"【潮州市】广东潮州市公司-林洪耿（0768-3275198|0768-2337000） 已揽收","AcceptTime":"2022-04-22 20:06:48","Remark":"","Location":"潮州市"},{"Action":"2","AcceptStation":"【潮州市】已离开 广东潮州市公司；发往 湖北武汉地区包","AcceptTime":"2022-04-22 20:12:48","Remark":"","Location":"潮州市"},{"Action":"2","AcceptStation":"【揭阳市】已到达 广东揭阳分拨交付中心","AcceptTime":"2022-04-22 21:34:02","Remark":"","Location":"揭阳市"},{"Action":"2","AcceptStation":"【揭阳市】已离开 广东揭阳分拨交付中心；发往 湖北武汉分拨交付中心","AcceptTime":"2022-04-22 21:36:22","Remark":"","Location":"揭阳市"},{"Action":"2","AcceptStation":"已到达 湖北武汉分拨交付中心","AcceptTime":"2022-04-23 20:22:37","Remark":"","Location":"武汉市"},{"Action":"2","AcceptStation":"已离开 湖北武汉分拨交付中心；发往 湖北武汉三角湖公司","AcceptTime":"2022-04-23 20:49:26","Remark":"","Location":"武汉市"},{"Action":"2","AcceptStation":"已到达 湖北武汉三角湖公司[027-59621483]","AcceptTime":"2022-04-24 08:27:58","Remark":"","Location":"武汉市"}]';

  //      var_dump(json_decode($arr,JSON_UNESCAPED_UNICODE));die();

        $params = $request->param();
        $user = $auth->user();
        $params['tenant_id'] = $user['tenant_id'];

        //添加商品
        $goodsService = new externalGoodsService();
        $res = $goodsService->createGoods($params, 'add');

        return $res;
    }

    //修改商品
    public function update($id, CatchAuth $auth, GoodsCreateRequest $request)
    {
        $params = $request->param();
        $user = $auth->user();

        $params['tenant_id'] = $user['tenant_id'];
        $params['id'] = $id;

        //修改商品
        $goodsService = new externalGoodsService();
        $res = $goodsService->editGoods($params, 'edit');

        return $res;
    }

    //删除商品
    public function delete($id, CatchAuth $auth, Request $request)
    {
        try {
            $goodsService = new GoodsService();
            $result = $goodsService->deleteGoods($id);
            if($result === true){
                return CatchResponse::success([], '删除成功');
            }else{
                return CatchResponse::fail('删除失败: ' . $result);
            }
        } catch (\Exception $exception) {
            return CatchResponse::fail('删除失败: ' . $exception->getMessage());
        }
    }
}